
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.CategoryThreeEntity;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class PostViewBinder
    extends ItemViewBinder<CategoryThreeEntity, PostViewBinder.ViewHolder> implements View.OnClickListener{

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_category_top, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull CategoryThreeEntity post) {
        holder.setData(post);
        holder.itemView.setTag(R.id.item_key_status,post);
        holder.itemView.setOnClickListener(this);
        if(post.isMoml()){
            holder.momlFlag.setVisibility(View.GONE);
            holder.itemView.setTag(R.id.item_key_position,-1);
        }else{
            holder.momlFlag.setVisibility(View.VISIBLE);
            holder.itemView.setTag(R.id.item_key_position,1);
        }
        ImageLoader.getInstance().displayImage(post.getIconUrl(),holder.cover);
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);

            if (onRecyclerViewItemClickListener != null && o instanceof CategoryThreeEntity){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView cover;
        private TextView title,momlFlag;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            cover = (ImageView) itemView.findViewById(R.id.iv_img);
            title = (TextView) itemView.findViewById(R.id.tv_name);
            momlFlag = (TextView) itemView.findViewById(R.id.tv_muslim);

        }


        void setData(@NonNull final CategoryThreeEntity post) {
//            cover.setImageResource(post.coverResId);
            title.setText(post.getName());
        }
    }
}
